package ch.zbw.pra.sysventory.presentation.panels;

import ch.zbw.pra.sysventory.application.controller.HomePanelController;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;

/**
 * Painted das HomePanel
 * 
 * @author Ralph Koster
 */

public class HomePanel extends VerticalLayout {

	private static final long serialVersionUID = -1187136726930833682L;
	private HomePanelController homePanelController;
	private HorizontalLayout titlePanel;
	private HorizontalLayout firstRowPanel;
	private HorizontalLayout secondRowPanel;
	private GridLayout scanInformationMainPanel;
	
	public HomePanel() {
		this.homePanelController = new HomePanelController();
		this.titlePanel = new HorizontalLayout();
		this.firstRowPanel = new HorizontalLayout();
		this.secondRowPanel = new HorizontalLayout();
		this.scanInformationMainPanel = new GridLayout(2, 1);
		
		paint();
	}
	
	private void paint() {
		this.setStyleName("home-panel");
		this.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		
		paintTitlePanel();
		paintScanInformationPanel();
		paintFirstRowPanel();
		paintSecondRowPanel();
	}
	
	private void paintTitlePanel() {
		titlePanel.setWidth(100, Sizeable.UNITS_PERCENTAGE);
		titlePanel.setSpacing(true);
		titlePanel.addStyleName("titlePanel");
        
        Label title = new Label("Sysventory Hard- und Software Inventarisierung - Home");
        title.setSizeUndefined();
        title.addStyleName("h1");
        
        titlePanel.addComponent(title);
        titlePanel.setComponentAlignment(title, Alignment.MIDDLE_CENTER);
        titlePanel.setExpandRatio(title, 1);
        
        this.addComponent(titlePanel);
	}
	
	private void paintFirstRowPanel() {
		firstRowPanel.setSizeFull();
		firstRowPanel.setMargin(new MarginInfo(true, true, true, true));
		firstRowPanel.setSpacing(true);
        firstRowPanel.addComponent(paintToolInformationPanel());
        firstRowPanel.addComponent(scanInformationMainPanel);
        
        this.addComponent(firstRowPanel);
        this.setExpandRatio(firstRowPanel, 1.5f);
	}

	private Component paintToolInformationPanel() {
		GridLayout mainPanel = new GridLayout(2, 3);
		
		mainPanel.setCaption("Tool Informationen");
		mainPanel.addStyleName("toolInformationPanel");
		mainPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		mainPanel.setColumnExpandRatio(0, 3);
		mainPanel.setColumnExpandRatio(1, 8);
		mainPanel.setHeight("160px");
		
		mainPanel.addComponent(new Label("Version:"));
		mainPanel.addComponent(new Label("0.0.1"));
		mainPanel.addComponent(new Label("Zweck:"));
		mainPanel.addComponent(new Label("Inventarisieren aller Hard- und Softwarekomponenten in einem Netzwerk"));

		return mainPanel;
	}

	public void paintScanInformationPanel() {
		GridLayout labelPanel = new GridLayout(1, 4);
		GridLayout textPanel = new GridLayout(1, 4);
		
		scanInformationMainPanel.removeAllComponents();
		scanInformationMainPanel.setCaption("Scan Informationen");
		scanInformationMainPanel.addStyleName("scanInformationPanel");
		scanInformationMainPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		scanInformationMainPanel.setHeight("160px");
		
		homePanelController.loadLastScanversion();
		
		labelPanel.addComponent(new Label("Letzter Scan gestartet am:"));
		labelPanel.addComponent(new Label("Letzter Scan abgeschlossen am:"));
		labelPanel.addComponent(new Label("Benötigte Zeit:"));
		labelPanel.addComponent(new Label("Nummer der letzten Scanversion:"));
		textPanel.addComponent(new Label(homePanelController.getLastScanStartDate()));
		textPanel.addComponent(new Label(homePanelController.getLastScanEndDate()));
		textPanel.addComponent(new Label(homePanelController.getLastScanDuration()));
		textPanel.addComponent(new Label(homePanelController.getLastScanNumber()));
		
		scanInformationMainPanel.addComponent(labelPanel);
		scanInformationMainPanel.addComponent(textPanel);
	}

	private void paintSecondRowPanel() {
		secondRowPanel.setSizeFull();
		secondRowPanel.setMargin(new MarginInfo(true, true, true, true));
		secondRowPanel.setSpacing(true);
		secondRowPanel.addComponent(paintDeveloperInfoPanel());
		secondRowPanel.addComponent(paintSchoolInfoPanel());
        this.addComponent(secondRowPanel);
        this.setExpandRatio(secondRowPanel, 1.5f);
	}
	
	private Component paintSchoolInfoPanel() {
		GridLayout mainPanel = new GridLayout(2, 1);
		GridLayout labelPanel = new GridLayout(1, 4);
		GridLayout textPanel = new GridLayout(1, 4);
		
		mainPanel.setCaption("Allgemeine Informationen");
		mainPanel.addStyleName("schoolInformationPanel");
		mainPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		mainPanel.setHeight("160px");
		
		labelPanel.addComponent(new Label("Schule:"));
		labelPanel.addComponent(new Label("Lehrgang:"));
		labelPanel.addComponent(new Label("Fach:"));
		labelPanel.addComponent(new Label("Lehrkraft:"));
		textPanel.addComponent(new Label("Zbw Zentrum für berufliche Weiterbildung"));
		textPanel.addComponent(new Label("HF Informatik 10/13"));
		textPanel.addComponent(new Label("Projektarbeit"));
		textPanel.addComponent(new Label("René Anderes"));
		
		mainPanel.addComponent(labelPanel);
		mainPanel.addComponent(textPanel);
		return mainPanel;
	}

	private Component paintDeveloperInfoPanel() {
		GridLayout mainPanel = new GridLayout(2, 1);
		GridLayout labelPanel = new GridLayout(1, 4);
		GridLayout textPanel = new GridLayout(1, 4);
		
		mainPanel.setCaption("Entwickler Informationen");
		mainPanel.addStyleName("developerInformationPanel");
		mainPanel.setWidth(97, Sizeable.UNITS_PERCENTAGE);
		mainPanel.setColumnExpandRatio(0, 3);
		mainPanel.setColumnExpandRatio(1, 8);
		mainPanel.setHeight("160px");
		
		labelPanel.addComponent(new Label("Softwareentwickler:"));
		labelPanel.addComponent(new Label(""));
		labelPanel.addComponent(new Label(""));
		labelPanel.addComponent(new Label("Systemtechniker:"));
		textPanel.addComponent(new Label("Ralph Koster"));
		textPanel.addComponent(new Label("Roberto Lucas"));
		textPanel.addComponent(new Label("Marcel Auer"));
		textPanel.addComponent(new Label("Dejan Krsmanovic"));
		
		mainPanel.addComponent(labelPanel);
		mainPanel.addComponent(textPanel);
		return mainPanel;
	}

	public void updateGUI() {
		paintScanInformationPanel();
	}	
}
